package mytask.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import mytask.dao.UserDAO;
import mytask.domain.AuthManager;
import mytask.domain.TranslationManager;
import mytask.model.User;
import mytask.model.UserLogin;
import mytask.view.Screen;
import mytask.view.ScreenManager;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

@Controller
public final class RegisterController extends MultiActionController {

	@Resource
	private UserDAO userDAO;

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public ModelAndView showRegisterScreen() {
		final ModelAndView screen = ScreenManager.get(Screen.REGISTER);

		final User newUser = new User();
		screen.addObject("newUser", newUser);

		return screen;
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ModelAndView registerUser(@RequestParam("pass") String pass, @Valid @ModelAttribute("newUser") User user,
			BindingResult results) {
		final ModelAndView screen = ScreenManager.get(Screen.REGISTER);

		boolean checkUser = true;
		final List<User> registeredUser = userDAO.listUsers();
		for (int i = 0; i < registeredUser.size(); i++) {
			if (user.getEmail().equals(registeredUser.get(i).getEmail())) {
				checkUser = false;
				break;
			}
		}

		if (checkUser) {
			if (pass.equals(user.getPassword()) && !results.hasErrors()) {
				screen.setViewName(ScreenManager.getViewName(Screen.HOME_GUEST));

				User newUser = new User();
				newUser.setEmail(user.getEmail());
				newUser.setFirstName(user.getFirstName());
				newUser.setLastName(user.getLastName());
				newUser.setPassword(AuthManager.getSHA1(user.getPassword()));
				newUser.setIsActive(true);

				userDAO.saveUser(newUser);

				final UserLogin login = new UserLogin();
				login.setEmail(user.getEmail());

				screen.addObject("login", login);
			} else if (!pass.equals(user.getPassword())) {
				results.rejectValue("password", "passwd_err", TranslationManager.get("message.password-mismatch"));
			}
		} else {
			if (!pass.equals(user.getPassword())) {
				results.rejectValue("password", "passwd_err", TranslationManager.get("message.password-mismatch"));
			}
			final String emailStatus = checkEmail(user.getEmail());
			screen.addObject("emailStatus", emailStatus);
		}

		return screen;
	}

	@RequestMapping(value = "/checkEmail", method = RequestMethod.POST)
	@ResponseBody
	public String checkEmail(@RequestParam("email") String email) {
		String status = "";
		User user = userDAO.getUserByEmail(email);

		if (user != null) {
			status = TranslationManager.get("message.user-exists");
		}

		return status;
	}
}